package com.bt7274.controller;

import com.bt7274.grace.result.GraceJSONResult;
import com.bt7274.pojo.bo.AdminLoginBO;
import com.bt7274.pojo.vo.AdminInfoVO;
import com.bt7274.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;

/**
 * 管理员控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/admin")
@Tag(name = "管理员管理", description = "管理员登录、信息查询等接口")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 管理员登录
     */
    @PostMapping("/login")
    @Operation(summary = "管理员登录", description = "管理员账号登录")
    public GraceJSONResult login(@Valid @RequestBody AdminLoginBO adminLoginBO,
                                 HttpServletRequest request) {
        try {
            AdminInfoVO adminInfo = adminService.login(adminLoginBO);

            // 将管理员信息保存到session中
            HttpSession session = request.getSession();
            session.setAttribute("adminId", adminInfo.getId());
            session.setAttribute("adminUsername", adminInfo.getUsername());
            session.setAttribute("isAdmin", true);

            return GraceJSONResult.ok(adminInfo);
        } catch (Exception e) {
            log.error("管理员登录失败: {}", e.getMessage(), e);
            return GraceJSONResult.errorMsg(e.getMessage());
        }
    }

    /**
     * 管理员登出
     */
    @PostMapping("/logout")
    @Operation(summary = "管理员登出", description = "清除管理员session信息")
    public GraceJSONResult logout(HttpServletRequest request) {
        try {
            HttpSession session = request.getSession(false);
            if (session != null) {
                String adminUsername = (String) session.getAttribute("adminUsername");
                session.invalidate();
                log.info("管理员 {} 已登出", adminUsername);
            }
            return GraceJSONResult.ok("管理员登出成功");
        } catch (Exception e) {
            log.error("管理员登出失败: {}", e.getMessage(), e);
            return GraceJSONResult.errorMsg(e.getMessage());
        }
    }

    /**
     * 获取当前管理员信息
     */
    @GetMapping("/info")
    @Operation(summary = "获取当前管理员信息", description = "根据session获取当前登录管理员的信息")
    public GraceJSONResult getCurrentAdminInfo(HttpServletRequest request) {
        try {
            HttpSession session = request.getSession(false);
            if (session == null) {
                return GraceJSONResult.errorMsg("管理员未登录");
            }

            String adminId = (String) session.getAttribute("adminId");
            if (adminId == null) {
                return GraceJSONResult.errorMsg("管理员未登录");
            }

            AdminInfoVO adminInfo = adminService.getAdminInfo(adminId);
            return GraceJSONResult.ok(adminInfo);
        } catch (Exception e) {
            log.error("获取管理员信息失败: {}", e.getMessage(), e);
            return GraceJSONResult.errorMsg(e.getMessage());
        }
    }
}